package com.example.chessbattleplatform.web.chess_rooms.dataService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chessbattleplatform.web.chess_rooms.Pojo.Enum.RoomUserColorEnum;
import com.example.chessbattleplatform.web.chess_rooms.Pojo.Enum.RoomUserEnum;
import com.example.chessbattleplatform.web.chess_rooms.Pojo.mappers.RoomMember;
import com.example.chessbattleplatform.web.chess_rooms.dataService.impl.RoomMemberDataServiceInterface;
import com.example.chessbattleplatform.web.chess_rooms.mapper.RoomMemberMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class RoomMemberDataService extends ServiceImpl<RoomMemberMapper, RoomMember> implements RoomMemberDataServiceInterface {

    private final RoomMemberMapper roomMemberMapper;

    //查询 房间成员信息
    @Override
    public List<RoomMember> getListUserRoomMember(String roomUuid) {
        LambdaQueryWrapper<RoomMember> eq = new LambdaQueryWrapper<>(RoomMember.class)
                .eq(RoomMember::getRoomUuid, roomUuid);
        return roomMemberMapper.selectList(eq);
    }

    //用户id 查询
    @Override
    public RoomMember getUserRoomMember(Long userid) {
        LambdaQueryWrapper<RoomMember> eq = new LambdaQueryWrapper<>(RoomMember.class)
                .eq(RoomMember::getUserId, userid);

        return roomMemberMapper.selectOne(eq);
    }

    // 加入 房间的用户
    @Override
    public Boolean joinRoom(RoomMember roomMember) {
        int insert = roomMemberMapper.insert(roomMember);
        return insert > 0;
    }

    // 统计 房间人数据
    @Override
    public Long CountNumberRoomMembers(String roomUuid) {
        LambdaQueryWrapper<RoomMember> eq = new LambdaQueryWrapper<>(RoomMember.class)
                .eq(RoomMember::getRoomUuid, roomUuid);
        return this.count(eq);
    }

    // 退出 房间的用户
    @Override
    public Boolean leaveRoom(Long userId) {
        LambdaQueryWrapper<RoomMember> eq = new LambdaQueryWrapper<>(RoomMember.class)
                .eq(RoomMember::getUserId, userId);
        return this.remove(eq);
    }

    // 更新 房间的用户
    @Override
    public Boolean updateRoomMember(RoomMember roomMember) {
        LambdaQueryWrapper<RoomMember> eq = new LambdaQueryWrapper<>(RoomMember.class)
                .eq(RoomMember::getUserId, roomMember.getUserId());
        return this.update(roomMember, eq);
    }

    // 退出并关闭等待房间
    @Override
    public Boolean quitAndCloseRoom(String roomUuid) {
        LambdaQueryWrapper<RoomMember> eq = new LambdaQueryWrapper<>(RoomMember.class)
                .eq(RoomMember::getRoomUuid, roomUuid);
        return roomMemberMapper.delete(eq) > 0;
    }

    //判断是否为房主
    @Override
    public Boolean ToDetermineWhetherYouAreTheHomeowner(Long userId) {

        LambdaQueryWrapper<RoomMember> eq = new LambdaQueryWrapper<>(RoomMember.class)
                .allEq(Map.of(RoomMember::getUserId, userId, RoomMember::getMemberType, RoomUserEnum.ROOM_OWNER));
        return roomMemberMapper.exists(eq);
    }

    //查询玩家是否在等待房间内
    public Boolean IsWhetherPlayerWaitingRoom(String roomId, Long userId) {
        LambdaQueryWrapper<RoomMember> lambdaQueryWrapper = new LambdaQueryWrapper<>(RoomMember.class)
                .allEq(Map.of(RoomMember::getRoomUuid, roomId, RoomMember::getUserId, userId));
        return this.exists(lambdaQueryWrapper);
    }

    // 查询 房间成员 中是否有指定 用户阵营 信息
    @Override
    public Boolean IsUserCampsWaitingRooms(String roomId, RoomUserColorEnum userColor) {

        LambdaQueryWrapper<RoomMember> lambdaQueryWrapper = new LambdaQueryWrapper<>(RoomMember.class)
                .allEq(Map.of(RoomMember::getRoomUuid, roomId, RoomMember::getUserColor, userColor));
        return this.exists(lambdaQueryWrapper);
    }

    @Override
    public Boolean IsUserEnumWaitingRooms(String roomId, RoomUserEnum memberType) {
        LambdaQueryWrapper<RoomMember> lambdaQueryWrapper = new LambdaQueryWrapper<>(RoomMember.class)
                .allEq(Map.of(RoomMember::getRoomUuid, roomId, RoomMember::getMemberType, memberType));
        return this.exists(lambdaQueryWrapper);
    }
}
